package com.lc.ibps.platform.rest.org;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.base.model.PartyEntity;
import com.lc.ibps.api.base.query.QueryFilter;
import com.lc.ibps.api.base.query.QueryOP;
import com.lc.ibps.api.org.service.IPartyPositionService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.core.util.string.StringUtil;
import com.lc.ibps.cloud.entity.APIPageList;
import com.lc.ibps.cloud.entity.APIRequest;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.org.party.persistence.entity.PartyPositionPo;
import com.lc.ibps.org.party.repository.PartyPositionRepository;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 岗位服务 控制器，第三方系统调用。
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：eddy
 * 邮箱：1546077710@qq.com
 * 日期：2016年9月22日-下午2:47:07
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */

@RequestMapping(value = "/api/webapi/positionService")
@Api(value = "/positionService", tags = "用户管理-岗位服务")
@RestController
public class PositionController extends BaseApiController {

	private IPartyPositionService positionService;
	private PartyPositionRepository partyPositionRepository;

	public PositionController() {
		positionService = AppUtil.getBean(IPartyPositionService.class);
		partyPositionRepository = AppUtil.getBean(PartyPositionRepository.class);
	}

	@ApiOperation(value = "根据ID获取岗位", notes = "根据ID获取岗位")
	@RequestMapping(value = "/loadById", method = RequestMethod.GET)
	public APIResult<PartyEntity> loadById(
			@ApiParam(value = "id", required = true) @RequestParam(name = "id", required = true) String id) {
		APIResult<PartyEntity> result = new APIResult<PartyEntity>();
		try {
			PartyEntity entity = positionService.getById(id);
			result.setData(entity);

		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取岗位失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据别名获取岗位", notes = "根据别名获取岗位")
	@RequestMapping(value = "/loadByAlias", method = RequestMethod.GET)
	public APIResult<PartyEntity> loadByAlias(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<PartyEntity> result = new APIResult<PartyEntity>();
		try {
			PartyEntity entity = positionService.getByAlias(alias);
			result.setData(entity);

		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取岗位失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户ID获取主岗位", notes = "根据用户ID获取主岗位")
	@RequestMapping(value = "/loadMainPostByUserId", method = RequestMethod.GET)
	public APIResult<PartyEntity> loadMainPostByUserId(
			@ApiParam(value = "userId", required = true) @RequestParam(name = "userId", required = true) String userId) {

		APIResult<PartyEntity> result = new APIResult<PartyEntity>();
		try {
			PartyEntity entity = positionService.findMainPostByUserId(userId);
			result.setData(entity);

		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取岗位失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "获取岗位列表", notes = "获取岗位列表")
	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findAll() {
		APIResult<List<PartyEntity>> result = new APIResult<>();
		try {
			List<PartyEntity> entity = positionService.findAll();
			
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取岗位列表失败！", e);
		}
		return result;
	}

	/*
	 * @Path("/findByPosRelation")
	 * 
	 * @ApiOperation(value = "获取岗位列表", notes = "根据关系获取岗位列表")
	 * 
	 * @GET
	 * 
	 * @Produces(MediaType.APPLICATION_JSON)
	 * 
	 * @RequestMapping("/findByPosRelation")
	 * 
	 * @ResponseBody public WebAPIResult findByPosRelation(
	 * 
	 * @QueryParam("alias") @ApiParam(value = "alias", required = true) String
	 * alias ,@QueryParam("relTypeKey") @ApiParam(value = "relTypeKey", required
	 * = true) String relTypeKey ,@QueryParam("relPartyName") @ApiParam(value =
	 * "relPartyName", required = true) String relPartyName
	 * ,@QueryParam("relLevel") @ApiParam(value = "relLevel", required = false,
	 * defaultValue = "1") int relLevel
	 * ,@QueryParam("isRecursion") @ApiParam(value = "isRecursion", required =
	 * false, defaultValue = "false") boolean isRecursion ) { List<PartyEntity>
	 * entity = positionService.getByPosRelation(alias, relTypeKey,
	 * relPartyName, relLevel, isRecursion);
	 * 
	 * WebAPIResult result = new WebAPIResult(); result.setData(entity);
	 * 
	 * return result; }
	 */

	@ApiOperation(value = "根据用户关系获取岗位列表", notes = "根据用户关系获取岗位列表")
	@RequestMapping(value = "/findByUserRelation", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByUserRelation(
			@ApiParam(value = "account", required = true) @RequestParam(name = "account", required = true) String account,
			@ApiParam(value = "relTypeKey", required = true) @RequestParam(name = "relTypeKey", required = true) String relTypeKey) {
		APIResult<List<PartyEntity>> result = new APIResult<>();
		try {
			List<PartyEntity> entity = positionService.getByUserRelation(account, relTypeKey);
			
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取岗位列表失败！", e);
		}
		return result;
	}

	/*
	 * @Path("/findByAttrKeyValue")
	 * 
	 * @ApiOperation(value = "根据属性获取岗位列表", notes = "根据属性获取岗位列表")
	 * 
	 * @GET public WebAPIResult findByAttrKeyValue(
	 * 
	 * @QueryParam("key") @ApiParam(value = "key", required = true) String key
	 * ,@QueryParam("value") @ApiParam(value = "value", required = true) Object
	 * value ) { List<PartyEntity> entity =
	 * positionService.getByAttrKeyValue(key, value);
	 * 
	 * WebAPIResult result = new WebAPIResult(); result.setData(entity);
	 * 
	 * return result; }
	 */

	@ApiOperation(value = "根据用户账号获取岗位列表", notes = "根据用户账号获取岗位列表")
	@RequestMapping(value = "/findByUserAccount", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByUserAccount(
			@ApiParam(value = "account", required = true) @RequestParam(name = "account", required = true) String account) {
		APIResult<List<PartyEntity>> result = new APIResult<>();
		try {
			List<PartyEntity> entity = positionService.getByUserAccount(account);
			
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取岗位列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据用户ID获取岗位列表", notes = "根据用户ID获取岗位列表")
	@RequestMapping(value = "/findByUserId", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByUserId(
			@ApiParam(value = "userId", required = true) @RequestParam(name = "userId", required = true) String userId) {

		APIResult<List<PartyEntity>> result = new APIResult<>();
		try {
			List<PartyEntity> entity = positionService.getByUserId(userId);
			
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取岗位列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据岗位关系获取岗位列表", notes = "根据岗位关系获取岗位列表")
	@RequestMapping(value = "/findByPosRel", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByPosRel(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias,
			@ApiParam(value = "relTypeKey", required = true) @RequestParam(name = "relTypeKey", required = true) String relTypeKey,
			@ApiParam(value = "relPartyName", required = true) @RequestParam(name = "relPartyName", required = true) String relPartyName) {

		APIResult<List<PartyEntity>> result = new APIResult<>();
		try {
			List<PartyEntity> entity = positionService.findByPosRel(alias, relTypeKey, relPartyName);
			
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取岗位列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据父节点ID获取子岗位列表", notes = "根据父节点ID获取子岗位列表")
	@RequestMapping(value = "/queryByParentId", method = RequestMethod.POST)
	public APIResult<APIPageList<PartyPositionPo>> queryByParentId(
			@ApiParam(name = "request", value = "传入查询请求json字符串", required = true) @RequestBody(required = true) APIRequest request) {
		APIResult<APIPageList<PartyPositionPo>> result = new APIResult<>();
		try {
			List<PartyPositionPo> entity = null;
			QueryFilter queryFilter = getQuerFilter(request);

			if (StringUtil.isNotEmpty(request.getParameter("name"))) {
				queryFilter.addFilter("NAME_", "%" + request.getParameter("name") + "%", QueryOP.LIKE);
				entity = partyPositionRepository.query(queryFilter);
			} else {
				queryFilter.addFilter("ID_", request.getParameter("parentId"), QueryOP.EQUAL);
				entity = // partyPositionRepository.findByParentId(parentId);
						partyPositionRepository.queryByParentId(request.getParameter("parentId"), queryFilter.getPage());
			}

//			List<PartyPositionPo> list = new ArrayList<PartyPositionPo>();
			for (PartyEntity partyEntity : entity) {
				PartyPositionPo po = (PartyPositionPo) partyEntity;
				String l = positionService.findByParentIdJson(po.getId());
				if (StringUtil.isNotEmpty(l)) {
					po.setHasChild(true);
				} else {
					po.setHasChild(false);
				}
//				list.add(po);
			}
			APIPageList<PartyPositionPo> pageList = getAPIPageList(entity);
			result.setData(pageList);
			result.setMessage("获取岗位列表数据成功");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取岗位列表数据失败", e);
		}

		return result;
	}

}
